﻿(
    function ()
    {
        //扩展分页工具栏
        Ext.define
        (
            'SPMS.PagingToolbar',
            {
                extend: 'Ext.PagingToolbar',

                //初始化组件
                initComponent: function ()
                {
                    //获得当前组件
                    var me = this;

                    //应用配置
                    Ext.apply
                    (
                       this,
                       {
                           cls: 'spms-grid-paging',
                           displayInfo: true,
                           displayMsg: '第 {0} - {1} 条记录，共 {2} 条记录',
                           emptyMsg: '没有数据',
                           plugins:
                           [
                               new Ext.create( 'Ext.ux.PagingToolbarResizer' )
                           ]
                       }
                    );
                    this.callParent();
                }
            }
        );

        //扩展行展开插件
        Ext.define
        (
            'SPMS.TreeComboBox',
            {
                extend: Ext.ux.TreePicker,

                //别名
                xtype: 'treecombobox',

                //下拉树样式
                pickerCls: undefined,

                //树是否使用箭头
                pickerUseArrows: false,

                //树面板是否显示列线
                pickerColumnLines: true,

                //树面板是否显示行线
                pickerRowLines: false,

                //根节点是否可见
                pickerRootVisible: true,

                //创建选择器
                createPicker: function ()
                {
                    //当前对象
                    var me = this;

                    //创建选择器
                    var picker = new Ext.tree.Panel
                    (
                        {
                            shrinkWrapDock: 2,
                            store: me.store,
                            floating: true,
                            useArrows: me.pickerUseArrows,
                            rootVisible: me.pickerRootVisible,
                            border: true,
                            columnLines: me.pickerColumnLines,
                            rowLines: me.pickerRowLines,
                            displayField: me.displayField,
                            columns: me.columns,
                            minHeight: me.minPickerHeight,
                            maxHeight: me.maxPickerHeight,
                            manageHeight: false,
                            cls: me.pickerCls,
                            shadow: false,
                            listeners:
                            {
                                scope: me,
                                itemclick: me.onItemClick
                            },
                            viewConfig:
                            {
                                listeners:
                                {
                                    scope: me,
                                    render: me.onViewRender
                                }
                            }
                        }
                    );

                    //获得视图对象
                    var view = picker.getView();

                    //对IE做特殊处理
                    if ( Ext.isIE9 && Ext.isStrict )
                    {
                        // In IE9 strict mode, the tree view grows by the height of the horizontal scroll bar when the items are highlighted or unhighlighted.
                        // Also when items are collapsed or expanded the height of the view is off. Forcing a repaint fixes the problem.
                        view.on
                        (
                            {
                                scope: me,
                                highlightitem: me.repaintPickerView,
                                unhighlightitem: me.repaintPickerView,
                                afteritemexpand: me.repaintPickerView,
                                afteritemcollapse: me.repaintPickerView
                            }
                        );
                    }

                    //返回选择器
                    return picker;
                },

                //初始化事件
                initEvents: function ()
                {
                    var me = this;
                    me.callParent();

                    // 挂接事件
                    me.keyNav = new Ext.util.KeyNav
                    (
                        me.inputEl,
                        {
                            down: me.onDownArrow,
                            esc:
                            {
                                handler: me.onEsc,
                                scope: me,
                                defaultEventAction: false
                            },
                            scope: me,
                            forceKeyDown: true
                        }
                    );

                    //如果不允许编辑，则输入框点击时展开下拉
                    if ( !me.editable )
                    {
                        me.mon( me.inputEl, 'click', me.onTriggerClick, me );
                    }

                    //禁用自动完成
                    if ( Ext.isGecko )
                    {
                        me.inputEl.dom.setAttribute( 'autocomplete', 'off' );
                    }
                }
            }
        );

        //扩展行展开插件
        Ext.define
        (
            'SPMS.RowExpander',
            {
                extend: Ext.grid.plugin.RowExpander,

                //是否显示扩展器
                hasExpander: true,

                //是否只允许展开一行
                isOnlyOneExpand: true,

                //记录最后展开的行
                lastExpanedRow: null,

                //判断指定的行是否已经收缩
                isCollapsed: function ( rowIdx )
                {
                    //获得当前对象
                    var me = this;

                    //获得视图
                    var view = me.view;

                    //获得行节点
                    var rowNode = view.getNode( rowIdx );

                    //获得行对象
                    var row = Ext.fly( rowNode, '_rowExpander' );

                    //返回状态
                    return row.hasCls( me.rowCollapsedCls );
                },

                //添加扩展器
                addExpander: function ()
                {
                    //获得当前对象
                    var me = this;

                    //判断是否添加扩展器
                    if ( me.hasExpander )
                    {
                        //调用基类实现
                        me.callParent( arguments );
                    }
                },

                //获得表格头定义
                getHeaderConfig: function ()
                {
                    //保存当前对象
                    var me = this;

                    //返回配置项
                    var config =
                    {
                        width: 30,
                        lockable: false,
                        sortable: false,
                        resizable: false,
                        draggable: false,
                        hideable: false,
                        menuDisabled: true,
                        tdCls: Ext.baseCSSPrefix + 'grid-cell-special',
                        innerCls: Ext.baseCSSPrefix + 'grid-cell-inner-row-expander',
                        renderer: function ( value, metadata )
                        {
                            if ( !me.grid.ownerLockable )
                            {
                                metadata.tdAttr += ' rowspan="2"';
                            }
                            return '<div class="' + Ext.baseCSSPrefix + 'grid-row-expander"></div>';
                        },
                        processEvent: function ( type, view, cell, rowIndex, cellIndex, e, record )
                        {
                            if ( type == "mousedown" && e.getTarget( '.x-grid-row-expander' ) )
                            {
                                me.toggleRowEx( rowIndex, record );
                                return me.selectRowOnExpand;
                            }
                        }
                    };

                    //返回配置项
                    return config;
                },

                //处理按键按下事件
                onKeyDown: function ( view, record, row, rowIdx, e )
                {
                    if ( e.getKey() == e.ENTER )
                    {
                        var ds = view.store,
                            sels = view.getSelectionModel().getSelection(),
                            ln = sels.length,
                            i = 0;

                        for ( ; i < ln; i++ )
                        {
                            rowIdx = ds.indexOf( sels[i] );
                            this.toggleRowEx( rowIdx, sels[i] );
                        }
                    }
                },

                //处理双击事件
                onDblClick: function ( view, record, row, rowIdx, e )
                {
                    this.toggleRowEx( rowIdx, record );
                },

                //折叠所有的行
                collapseAll: function ()
                {
                    //保存当前对象
                    var me = this;

                    //获得视图
                    var view = me.view;

                    //获得存储对象
                    var store = view.store;

                    //循环处理所有的行
                    for ( var index = 0; index < store.getCount() ; index++ )
                    {
                        //获得记录
                        var record = store.getAt( index );

                        //判断是否已经展开
                        if ( !me.isCollapsed( index ) )
                        {
                            //收缩行
                            me.toggleRowEx( index, record );
                        }
                    }
                },

                //展开行
                expand: function ( rowIdx, record )
                {
                    //保存当前对象
                    var me = this;

                    //判断处于收缩状态
                    if ( me.isCollapsed( rowIdx ) )
                    {
                        //展开
                        me.toggleRowEx( rowIdx, record );
                    }
                },

                //处理行折叠或者展开
                toggleRowEx: function ( rowIdx, record )
                {
                    //保存当前对象
                    var me = this;

                    //判断是否只允许展开一行
                    if ( me.isOnlyOneExpand )
                    {
                        //获得视图
                        var view = me.view;
                        //获得行节点
                        var rowNode = view.getNode( rowIdx );
                        //获得行对象
                        var row = Ext.fly( rowNode, '_rowExpander' );
                        //判断是否已收缩
                        var isCollapsed = row.hasCls( me.rowCollapsedCls );

                        //判断是否展开
                        if ( isCollapsed )
                        {
                            //判断是否有已经展开的行
                            if ( me.lastExpanedRow != null )
                            {
                                try
                                {
                                    me.toggleRow( me.lastExpanedRow.rowIdx, me.lastExpanedRow.record );
                                }
                                catch ( error )
                                {
                                    alert( error );
                                }
                            }

                            //记录最后展开的行
                            me.lastExpanedRow = { rowIdx: rowIdx, record: record };
                        }
                        else if ( null != me.lastExpanedRow && me.lastExpanedRow.rowIdx === rowIdx )
                        {
                            //如果收缩最后展开的行，则清除数据
                            me.lastExpanedRow = null;
                        }
                    }

                    //调用基类实现
                    me.toggleRow( rowIdx, record );
                },

                //折叠行
                toggleRow: function ( rowIdx, record )
                {
                    var me = this;
                    var view = me.view;
                    var rowNode = view.getNode( rowIdx );
                    var row = Ext.fly( rowNode, '_rowExpander' );
                    var nextBd = row.down( me.rowBodyTrSelector, true );
                    var isCollapsed = row.hasCls( me.rowCollapsedCls );
                    var addOrRemoveCls = isCollapsed ? 'removeCls' : 'addCls';
                    var ownerLock;
                    var rowHeight;
                    var fireView;


                    Ext.suspendLayouts();
                    row[addOrRemoveCls]( me.rowCollapsedCls );
                    Ext.fly( nextBd )[addOrRemoveCls]( me.rowBodyHiddenCls );
                    me.recordsExpanded[record.internalId] = isCollapsed;
                    view.refreshSize();

                    if ( me.grid.ownerLockable )
                    {
                        ownerLock = me.grid.ownerLockable;
                        fireView = ownerLock.getView();
                        view = ownerLock.lockedGrid.view;
                        rowHeight = row.getHeight();
                        row = Ext.fly( view.getNode( rowIdx ), '_rowExpander' );
                        row.setHeight( rowHeight );
                        row[addOrRemoveCls]( me.rowCollapsedCls );
                        view.refreshSize();
                    } else
                    {
                        fireView = view;
                    }
                    fireView.fireEvent( isCollapsed ? 'expandbody' : 'collapsebody', row.dom, record, nextBd );

                    Ext.resumeLayouts( true );
                }
            }
        );
    }
)();